﻿var app;
app = app || (function () {
    var defaultHeaderText;
    var isConfirmDialogShown = false;

    return {
        language: 'en',

        showPleaseWait: function (headerText) {

            //set default text
            if (!defaultHeaderText) {
                $('#ajax-loading .modal-header .modal-title').text();
            }

            //update header text, if it's defined
            if (headerText) {
                $('#ajax-loading .modal-header .modal-title').text(headerText);
            } else {
                $('#ajax-loading .modal-header .modal-title').text(defaultHeaderText);
            }

            $('#ajax-loading').modal();
        },

        hidePleaseWait: function () {
            $('#ajax-loading').modal('hide');
        },

        showErrorDialog: function (errors, options) {

            var defaultOptions = {
                headerText: 'Error'
            };
            var opt = $.extend({}, defaultOptions, options);

            $('#error-dialog .modal-header .modal-title').html(opt.headerText);
            $("#error-dialog .modal-body > ul").empty();
            $.each(errors, function (index, value) {
                $('#error-dialog .modal-body > ul').append(
                    $('<li>').append(value));
            });

            $("#error-dialog").modal("show");

        },

        showConfirmDialog: function (callbacks, options) {

            var defaultCallbacks = {
                yes: undefined,
                no: undefined
            };

            var defaultOptions = {
                showHeader: false,
                headerText: 'Are you sure?',
                bodyText: 'Are you sure?'
            };

            var callb = $.extend({}, defaultCallbacks, callbacks);
            var opt = $.extend({}, defaultOptions, options);

            if (opt.showHeader) {
                $('#confirm-dialog .modal-header').toggleClass('hidden', false);
                $('#confirm-dialog .modal-header .modal-title').html(opt.headerText);
            } else {
                $('#confirm-dialog .modal-header').toggleClass('hidden', true);
            }

            if (opt.bodyText) {
                $('#confirm-dialog .modal-body').html($("<p>").html(opt.bodyText));
            } else {
                $('#confirm-dialog .modal-body').html('');
            }

            //callbacks
            $('#confirm-dialog .modal-footer button[name="yes"]').off('click').on('click', function () {

                if (callb.yes) {
                    callb.yes();
                }
            });

            $('#confirm-dialog .modal-footer button[name="no"]').off('click').on('click', function () {

                if (callb.no) {
                    callb.no();
                }
            });

            //events
            $('#confirm-dialog').off('show.bs.modal').on('show.bs.modal', function () {
                isConfirmDialogShown = true;
            });
            $('#confirm-dialog').off('hidden.bs.modal').on('hidden.bs.modal', function () {
                isConfirmDialogShown = false;
            });
            var showModal = true;
            if (isConfirmDialogShown) {

                $('#confirm-dialog').modal('hide');
                $('#confirm-dialog').off('hidden.bs.modal').on('hidden.bs.modal', function () {
                    isConfirmDialogShown = false;
                    if (showModal) {
                        showModal = false;
                        $('#confirm-dialog').modal('show');
                    }
                });

            } else {

                $('#confirm-dialog').modal('show');

            }

        }

    };
})();

$.ajaxSetup({
    cache: false,
    //Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror"
    error: function (xhr, status) {

        if (status == 'abort') return;

        var json = null;
        try {
            json = $.parseJSON(xhr.responseText);
        }
        catch (exception) {
        }

        var globalErrors = [];

        if (json == null) {

            globalErrors.push(xhr.status + ': ' + xhr.statusText);

        } else {

            if (!json.IsValid && json.Errors) {

                $.each(json.Errors, function (key, value) {

                    $.each(value, function (index2, value2) {
                        globalErrors.push(value2);
                    });

                });

            }

        }

        app.showErrorDialog(globalErrors);

    }
});